package com.go.where.manager.service.system.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.go.where.manager.dao.system.MenuDao;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.user.Menu;
import com.go.where.manager.service.system.MenuService;
import com.go.where.manager.util.CommonUtil;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

	@Resource(name = "menuDao")
	private MenuDao menuDao;

	@Override
	public int save(Menu menu) {
		menu.setMenuId(CommonUtil.createRandomNumber());
		for (int i = 0; i < 20; i++) {
			Menu pDB = menuDao.getByKey(menu.getMenuId());
			if (pDB != null) {
				menu.setMenuId(CommonUtil.createRandomNumber());
			} else {
				break;
			}
		}
		menu.setInsertTime(new Date());
		return this.menuDao.insert(menu);
	}

	@Override
	public Pagination<Menu> queryPage(Menu menu) {
		Integer rowCount = menuDao.totalCount(menu);
		menu.calculate();
		List<Menu> list = menuDao.queryList(menu);
		Pagination<Menu> page = this.menuDao.queryPagination(
				rowCount, list, menu);
		return page;
	}

	@Override
	public Menu queryById(String menuId) {
		return this.menuDao.getByKey(menuId);
	}

	@Override
	public int update(Menu menu) {
		menu.setUpdateTime(new Date());
		return this.menuDao.update(menu);
	}

	@Override
	public int delete(String menuId) {
		return this.menuDao.deleteByKey(menuId);
	}

}
